package pearson.automation;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;

import org.testng.annotations.DataProvider;

public class XlsDataProvider {
    @DataProvider(name = "getUploadFiles")
    static Object[][] makeFileData() throws Exception {
        Object[][] retObjArr = getTableArray("UploadFiles.xls", "file");
        return retObjArr;
    }

    @DataProvider(name = "getUploadVideos")
    static Object[][] makeVideoData() throws Exception {
        Object[][] retObjArr = getTableArray("UploadFiles.xls", "video");
        return retObjArr;
    }

    @DataProvider(name = "getUploadVideoLinks")
    static Object[][] makeLinkData() throws Exception {
        Object[][] retObjArr = getTableArray("UploadFiles.xls", "link");
        return retObjArr;
    }

    @DataProvider(name = "getNotes")
    static Object[][] makeNoteData() throws Exception {
        Object[][] retObjArr = getTableArray("RightPanelNotes.xls", "notes");
        return (retObjArr);
    }

    protected static String[][] getTableArray(
            String xlFilePath,
            String sheetName) throws Exception {
        String[][] tabArray = null;
        Workbook workbook = Workbook.getWorkbook(
                XlsDataProvider.class.getClassLoader().
                    getResourceAsStream(xlFilePath));
        Sheet sheet = workbook.getSheet(sheetName);
        // Total Total No Of Rows in Sheet, will return you no of rows that are
        // occupied with some data
        int rowCount = sheet.getRows();

        // Total Total No Of Columns in Sheet
        int columnCount = sheet.getColumns();

        // Reading Individual Row Content
        tabArray = new String[rowCount][columnCount];
        for (int i = 0; i < rowCount; i++) {        
            // Get Individual Row
            Cell[] rowData = sheet.getRow(i);
            for (int j = 0; j < columnCount; j++) {
                tabArray[i][j] = rowData[j].getContents();
            }
        }
        workbook.close();
        return (tabArray);
    }
}
